/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.shiro.realm.AuthorizingRealm;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.kj.entity.KjMemberIntegrals;
import com.jeesite.modules.kj.entity.KjSkill;
import com.jeesite.modules.kj.member.entity.MemberUser;
import com.jeesite.modules.kj.service.KjMemberIntegralsService;
import com.jeesite.modules.kj.service.KjSkillService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.service.KjMemberService;

import java.util.List;

/**
 * kj_memberController
 * @author hzy
 * @version 2021-07-04
 */
@Controller
@RequestMapping(value = "${adminPath}/kj/kjMember")
public class KjMemberController extends BaseController {

	@Autowired
	private KjMemberService kjMemberService;

	@Autowired
	private KjSkillService kjSkillService;
	@Autowired
	private KjMemberIntegralsService kjMemberIntegralsService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public KjMember get(String id, boolean isNewRecord) {
		return kjMemberService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("kj:kjMember:view")
	@RequestMapping(value = {"list", ""})
	public String list(KjMember kjMember, Model model) {
		model.addAttribute("kjMember", kjMember);
		return "modules/kj/kjMemberList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("kj:kjMember:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<KjMember> listData(KjMember kjMember, HttpServletRequest request, HttpServletResponse response) {
		kjMember.setPage(new Page<>(request, response));
		Page<KjMember> page = kjMemberService.findPage(kjMember);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("kj:kjMember:view")
	@RequestMapping(value = "form")
	public String form(KjMember kjMember, Model model) {
		List<KjSkill> skillList = kjSkillService.findList(new KjSkill());
		KjMemberIntegrals mi = kjMemberIntegralsService.findByMemberId(kjMember.getId());
		model.addAttribute("skillList", skillList);
		model.addAttribute("kjMember", kjMember);

		model.addAttribute("totalHour", mi.getTotalHours());
		model.addAttribute("totalIntegral", mi.getTotalIntegrals());
		return "modules/kj/kjMemberForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("kj:kjMember:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated KjMember kjMember) {
		kjMemberService.save(kjMember);
		return renderResult(Global.TRUE, text("保存kj_member成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("kj:kjMember:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(KjMember kjMember) {
		kjMemberService.delete(kjMember);
		return renderResult(Global.TRUE, text("删除kj_member成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("kj:kjMember:view")
	@RequestMapping(value = "kjMemberSelect")
	public String empUserSelect(KjMember kjMember, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("KjMember", kjMember);
		return "modules/kj/kjMemberSelect";
	}


	/**
	 * 停用用户
	 * @param MEMBER
	 * @return
	 */
	@RequiresPermissions("kj:kjMember:edit")
	@ResponseBody
	@RequestMapping(value = "disable")
	public String disable(KjMember member) {
		member.setStatus(User.STATUS_FREEZE);
		kjMemberService.updateStatus(member);
		return renderResult(Global.TRUE, text("冻结用户''{0}''成功", member.getName()));
	}

	/**
	 * 启用用户
	 * @param member
	 * @return
	 */
	@RequiresPermissions("kj:kjMember:edit")
	@ResponseBody
	@RequestMapping(value = "enable")
	public String enable(KjMember member) {
		member.setStatus(User.STATUS_NORMAL);
		kjMemberService.updateStatus(member);
		return renderResult(Global.TRUE, text("启用用户''{0}''成功", member.getName()));
	}


	/**
	 * 导出用户数据
	 */
	@RequiresPermissions("kj:kjMember:view")
	@RequestMapping(value = "exportData")
	public void exportData(KjMember kjMember, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		kjMember.setPageNo(null);
		kjMember.setPageSize(null);
		List<KjMember> list = kjMemberService.findList(kjMember);
		String fileName = "用户数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("用户数据", KjMember.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}
}
